package com.mountainreacher.automata;

import java.io.File;
import java.io.FileOutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.mountainreacher.automata.R;

import vista.AutomataResources;
import vista.InterfazControl;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.util.Xml;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity {

	private final String AD_UNIT_ID = "ca-app-pub-0360613478597349/9445683926";
	private AdView adView;

	private InterfazControl interfaz;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);

		FrameLayout frameLayout = (FrameLayout)findViewById(R.id.canvasframe);
		interfaz = new InterfazControl(this, new AutomataResources(getResources()));
		frameLayout.addView(interfaz);

		// Create the adView.
		adView = new AdView(this);
		adView.setAdUnitId(AD_UNIT_ID);
		adView.setAdSize(AdSize.BANNER);

		// Lookup your LinearLayout assuming it's been given
		LinearLayout layout = (LinearLayout)findViewById(R.id.OverlapLayout);

		// Add the adView to it.
		layout.addView(adView, 0);

		// Initiate a generic request.
		AdRequest adRequest = new AdRequest.Builder().build();

		// Load the adView with the ad request.
		adView.loadAd(adRequest);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {

		case R.id.reduce_fem:
			interfaz.reduceFem();
			break;

		case R.id.save_model:
			interfaz.saveModel();
			break;
			
		case R.id.load_example:
			interfaz.loadExample();
			break;

		}
		return true;
	}

	//	@Override
	//	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	//		interfaz.resume();
	//		
	//		if(resultCode == RESULT_OK) {
	//			Toast.makeText(this, "Result OK", 0).show();
	//		} else {
	//			Toast.makeText(this, "Other Result", 0).show();
	//		}
	////		interfaz.req.commitChanges((Request)data.getExtras().get(Request.REQ));
	//		
	////		interfaz.req.node.nombre = data.getEx
	//	}

	private void saveModel() {

		try {

			//			File sampleDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/automatas");
			//			sampleDir.mkdirs();
			//			
			//			FileOutputStream writer = openFileOutput("automata.txt", Context.MODE_APPEND);

			File sampleDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/automatas");
			sampleDir.mkdirs();

			File file;
			file = File.createTempFile("automata", ".txt", sampleDir);
			FileOutputStream writer = new FileOutputStream(file);

			org.xmlpull.v1.XmlSerializer serializer = Xml.newSerializer();
			serializer.setOutput(writer, "UTF-8");
			serializer.startDocument(null, true);
			serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);

			serializer.startTag(null, "root");

			serializer.startTag("", "Items");

			serializer.startTag("", "Points");
			serializer.attribute("", "PointValue", "punto");
			serializer.endTag("", "Points");

			serializer.endTag("", "Items");

			serializer.startTag("", "Items");
			serializer.startTag("", "Points");
			serializer.attribute("", "PointValue", "punto");
			serializer.endTag("", "Points");
			serializer.endTag("", "Items");

			serializer.endDocument();

			//        writer.toString();
			//        myFile.write(writer.toString9).getBytes());

		} catch(Exception e) {
			Toast.makeText(this, e.getMessage(), 0).show();
		}

	}

	public void doSomething() {

		Document doc = parseXML(new InputSource("hola.txt"));


	}


	public Document parseXML(InputSource source) {
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			dbf.setNamespaceAware(false);
			dbf.setValidating(false);
			DocumentBuilder db = dbf.newDocumentBuilder();
			return db.parse(source);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}


	// **************************************** APP LIFE CYCLE ***************************************

	@Override
	public void onPause() {
		adView.pause();
		interfaz.pause();
		super.onPause();
	}

	@Override
	public void onResume() {
		super.onResume();
		adView.resume();
		interfaz.resume();
	}

	@Override
	public void onDestroy() {
		adView.destroy();
		interfaz.destroy();
		super.onDestroy();
	}

	// **************************************** APP LIFE CYCLE ***************************************


}
